package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Account;
import com.example.entity.Teacher;
import com.example.exception.CustomException;
import com.example.mapper.TeacherMapper;
import com.example.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;
    @Override
    public void add(Teacher teacher) {
        //账号唯一验证
        Teacher dbTeacher = teacherMapper.selectByUsername(teacher.getUsername());
        if (ObjectUtil.isNotNull(dbTeacher)) {
            throw new CustomException("用户名已存在");
        }
        //密码为空时，默认密码为123456
        if (ObjectUtil.isEmpty(teacher.getPassword())) {
            teacher.setPassword("123456");
        }
        // 设置角色
        teacher.setRole("TEACHER");
        //新增
        teacherMapper.insert(teacher);
    }

    @Override
    public PageInfo<Teacher> selectPage(Teacher teacher, Integer pageNum, Integer pageSize) {
        List<Teacher> teacherList;
        PageHelper.startPage(pageNum, pageSize);//分页插件前置设置
        if(ObjectUtil.isNotEmpty(teacher.getName())){
            teacherList = teacherMapper.selectByName(teacher.getName());
        }else{
            teacherList = teacherMapper.selectAll();
        }
        return PageInfo.of(teacherList);
    }

    @Override
    public void update(Teacher teacher) {
        //新增的时候判断账号的唯一性
        //1.通过ID获取原teacher，然后判断当前的账号是否和原账号一致
        Teacher dbTeacher = teacherMapper.selectById(teacher.getId());
        //原账号
        String oldUsername = dbTeacher.getUsername();
        //新账号
        String newUsername = teacher.getUsername();
        if (!oldUsername.equals(newUsername)){
            Teacher myteacher = teacherMapper.selectByUsername(teacher.getUsername());
            if (ObjectUtil.isNotNull(myteacher)){
                throw new CustomException("用户名已存在");
            }
        }
        teacherMapper.updateById(teacher);
    }

    @Override
    public void deleteById(Integer id) {
        teacherMapper.deleteById(id);
    }

    @Override
    public Teacher login(Account account) {
        Teacher teacher = teacherMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(teacher)) {
            throw new CustomException("用户不存在");
        }
        if (!account.getPassword().equals(teacher.getPassword())) {
            throw new CustomException("账号或密码错误");
        }
        return teacher;
    }

    @Override
    public void updatePassword(Account account) {
        //获取原密码，判断原密码是否正确
        //1.从数据库获取原密码
        Teacher teacher = teacherMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(teacher)) {
            throw new CustomException("用户不存在");
        }
        if (!account.getPassword().equals(teacher.getPassword())) {
            throw new CustomException("原密码错误");
        }
        teacher.setPassword(account.getNewPassword());
        teacherMapper.updateById(teacher);
    }

    @Override
    public List<Teacher> selectAll() {
        return teacherMapper.selectAll();
    }
}
